﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SynchronizationContextDispatcher.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the SynchronizationContextDispatcher type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Threading
{
    using System;
    using System.Threading;
    using System.Windows.Threading;

    public class SynchronizationContextDispatcher : IDispatcher
   {
      #region Fields

      private readonly SynchronizationContext _context;

      #endregion

      public SynchronizationContextDispatcher(SynchronizationContext context)
      {
          _context = Guard.ArgumentNull("context", context);
      }

      public SynchronizationContextDispatcher() : this(SynchronizationContext.Current)
      {
      }

      #region ITaskResultDispatcher Members

      public void BeginInvoke(Action action)
      {
         _context.Post(s => action(), null);
      }

        public bool CheckAccess()
        {
            return SynchronizationContext.Current is DispatcherSynchronizationContext && 
                UIDispatcher.Current.CheckAccess();
        }

        #endregion
   }
}
